
package com.winway.WinCRM.application.impl.core;

import java.util.List;
import java.util.ArrayList;
import java.text.MessageFormat;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import com.dayatang.querychannel.support.Page;
import com.dayatang.querychannel.service.QueryChannelService;
import com.winway.WinCRM.application.dto.*;
import com.winway.WinCRM.application.core.TestApplication;
import com.winway.WinCRM.core.*;

@Named
@Transactional
public class TestApplicationImpl implements TestApplication {

	@Inject
	private QueryChannelService queryChannel;
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public TestDTO getTest(Long id) {
		Test test = Test.load(Test.class, id);
		TestDTO testDTO = new TestDTO();
		// 将domain转成VO
		try {
			BeanUtils.copyProperties(testDTO, test);
		} catch (Exception e) {
			e.printStackTrace();
		}
		testDTO.setId((java.lang.Long)test.getId());
		return testDTO;
	}
	
	public TestDTO saveTest(TestDTO testDTO) {
		Test test = new Test();
		try {
        	BeanUtils.copyProperties(test, testDTO);
        } catch (Exception e) {
        	e.printStackTrace();
        }
		test.save();
		testDTO.setId((java.lang.Long)test.getId());
		return testDTO;
	}
	
	public void updateTest(TestDTO testDTO) {
		Test test = Test.get(Test.class, testDTO.getId());
		// 设置要更新的值
		try {
			BeanUtils.copyProperties(test, testDTO);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void removeTest(Long id) {
		this.removeTests(new Long[] { id });
	}
	
	public void removeTests(Long[] ids) {
		for (int i = 0; i < ids.length; i++) {
			Test test = Test.load(Test.class, ids[i]);
			test.remove();
		}
	}
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<TestDTO> findAllTest() {
		List<TestDTO> list = new ArrayList<TestDTO>();
		List<Test> all = Test.findAll(Test.class);
		for (Test test : all) {
			TestDTO testDTO = new TestDTO();
			// 将domain转成VO
			try {
				BeanUtils.copyProperties(testDTO, test);
			} catch (Exception e) {
				e.printStackTrace();
			}
			list.add(testDTO);
		}
		return list;
	}
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public Page<TestDTO> pageQueryTest(TestDTO queryVo, int currentPage, int pageSize) {
		List<TestDTO> result = new ArrayList<TestDTO>();
		List<Object> conditionVals = new ArrayList<Object>();
	   	StringBuilder jpql = new StringBuilder("select _test from Test _test   where 1=1 ");
	
	   	if (queryVo.getUserName() != null && !"".equals(queryVo.getUserName())) {
	   		jpql.append(" and _test.UserName like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getUserName()));
	   	}		
	
        Page<Test> pages = queryChannel.queryPagedResultByPageNo(jpql.toString(), conditionVals.toArray(), currentPage, pageSize);
        for (Test test : pages.getResult()) {
            TestDTO testDTO = new TestDTO();
            
             // 将domain转成VO
            try {
            	BeanUtils.copyProperties(testDTO, test);
            } catch (Exception e) {
            	e.printStackTrace();
            } 
            
                                 result.add(testDTO);
        }
        return new Page<TestDTO>(pages.getCurrentPageNo(), pages.getTotalCount(), pages.getPageSize(), result);
	}
	
	
}
